package Mytest.day02;

import java.util.ArrayList;
import java.util.Scanner;

//添加老师:通过键盘录入老师信息(id，name，age，birthday)
// 使用数组存储老师信息,要求老师的id不能重复
// 删除老师:通过键盘录入要删除老师的id值,将该老师从数组中移除,如果录入的id在数组中不存在,需要重新录 入
// 修改老师:通过键盘录入要修改老师的id值和修改后的老师信息 将数组中该老师的信息修改,如果录入的id在数组中不存在,需要重新录入
// 查询老师:将数组中存储的所有老师的信息输出到控制台
public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    TeacherService teacherService = new TeacherService();

    public void start() {
        lo:
        while (true) {
            System.out.println("‐‐‐‐‐‐‐‐欢迎来到 <老师> 管理系统‐‐‐‐‐‐‐‐");
            System.out.println("请输入您的选择: 1.添加老师 2.删除老师 3.修改老师 4.查看老师 5.退 出");

            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case 2:
                    System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case 3:
                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case 4:
                    System.out.println("查看老师");
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("谢谢使用");

                    break lo;
            }
        }
    }

    public void findAllStudent() {
        ArrayList<Teacher> list = TeacherService.findAllTeacher();
        if (list == null) {
            System.out.println("查询无结果，请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Teacher t = list.get(i);
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }

    }

    public void updateTeacher() {
        String id = inputTeacher();
        Teacher newTeacher = inputTeacherInfo(id);
        teacherService.updateTeacher(id, newTeacher);
        System.out.println("修改成功！");
    }

    public void deleteTeacher() {
        String id;
        while (true) {
            System.out.println("请输入您要删除的id");
            id = sc.next();
            TeacherService teacherService = new TeacherService();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在,请重新输入");
            } else {
                break;
            }
        }
        boolean result = teacherService.deleteTeacher(id);
        if (result) {
            System.out.println("删除成功！！");
        }

    }

    public void addTeacher() {
        /*
        1.接收不存在的id
        2.将接收的信息封装为一个老师对象
        3.将对象传给Service
        4.在控制台打印成功/失败
        */
        String id;
//接收id
        while (true) {
            System.out.println("请输入您要添加的id");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("id已经被占用，请重新输入");

            } else {
                break;
            }
        }
        Teacher t = inputTeacherInfo(id);
        boolean result = teacherService.addTeacher(t);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }


    public String inputTeacher() {
        String id;
        while (true) {
            System.out.println("请输入您要修改的id");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入");

            } else {
                break;
            }
        }
        return id;
    }

    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        int age = sc.nextInt();
        System.out.println("请输入老师生日");
        String birthday = sc.next();
        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);
        return t;
    }
}
